﻿<?php 
class QuestionAnswerWidget extends CWidget{
		public $tutorial_id;
		public $questions;
		public $message;
		public $check;
		public $username;
		public $total;
		public $showIt = false;
		public $answers;
		
		public function init(){
			$this->answers = new ForumAnswers;
			$this->questions = ForumQuestionAssignment::model()->findAllByAttributes(array('tutorial_id'=>$this->tutorial_id));
			$this->total = Forum::model()->findAll(array('order'=>'DateCreated DESC'));
				if($this->questions == null){
					$this->message = 'No questions regarding this topic';
					$this->check = false;
					}
			}
			
		public function run(){
				$this->render('QuestionAnswerWidget');
			}
			
		public function getAnswers(){
				return $this->answers;
			}
			
		public function check(){
				return $this->check;
			}
			
		public function getMessage(){
				return $this->message;
			}
			
		public function getQuestions(){
				return $this->questions;
			}
			
		public function getUser($uid){
				$profile = UserProfiles::model()->findByAttributes(array('users_id'=>$uid));
				if($profile != null){
						$this->username = $profile->first_name . " " . $profile->last_name;
					}
				if($this->username == null){
					$this->username = Users::model()->findByPk($uid)->username;
					}
				return $this->username;
			}
			
		public function getTutorialId(){
				return $this->tutorial_id;
			}
			
		public function getQuestionCount(){
				$this->showIt = false;
				if(sizeof($this->questions) >= 3){
						$this->showIt = true;
					}
				$answers = array(); $counter = 0;
				if($this->questions != null){
					foreach($this->questions as $q){
						$answers[$counter] = array();
						$answers[$counter] = ForumAnswerAssignment::model()->findAllByAttributes(array('question_id'=>$q->question_id));
						$counter++;
						}
					}
					
				if(sizeof($answers) >= 10){
						$this->showIt = true;
					}
					
				return $this->showIt;
			}
			
		public function getQuestionAnswers($qid){
				return sizeof(ForumAnswerAssignment::model()->findAllByAttributes(array('question_id'=>$qid)));
			}
		
		public function getQuestionAnswersContent($qid){
				return ForumAnswerAssignment::model()->findAllByAttributes(array('question_id'=>$qid));
			}
	}
 ?>